package com.smh.aiagent.agent;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.tool.ToolCallback;

/**
 * @author shiminghui
 * @date 2025/6/8 16:45
 * @description: Agent
 */
public class SmhAgent extends ToolCallingAgent {

    private static final String DEFAULT_SYSTEM_PROMPT = """
            You are OpenManus, an all-capable AI assistant, aimed at solving any task presented by the user.
            You have various tools at your disposal that you can call upon to efficiently complete complex requests.
            If you can't complete the task, prioritize calling the 'doTerminate' tool or function to end the interaction.
            """;
    private static final String DEFAULT_NEXT_STEP_PROMPT = """
            Based on user needs, proactively select the most appropriate tool or combination of tools.
            For complex tasks, you can break down the problem and use different tools step by step to solve it.
            After using each tool, clearly explain the execution results and suggest the next steps.
            If you want to stop the interaction at any point, use the `doTerminate` tool/function call.
            If you can't complete the task, prioritize calling the 'doTerminate' tool or function to end the interaction.
            """;

    private static final String DEFAULT_STUCK_PROMPT = """
            Observed duplicate responses. Consider new strategies and avoid repeating ineffective paths already attempted.
            """;


    private SmhAgent(ToolCallback[] allTools) {
        super(allTools);
    }

    public static Builder builder() {
        return new Builder();
    }

    public static class Builder {
        private String name = "smhAgent";
        private String description;
        private String SystemPrompt = DEFAULT_SYSTEM_PROMPT;
        private String nextStepPrompt = DEFAULT_NEXT_STEP_PROMPT;
        private String stuckPrompt = DEFAULT_STUCK_PROMPT;
        private ChatClient chatClient;
        private ToolCallback[] availableTools = new ToolCallback[0];
        private Integer maxStep = 10;


        public Builder name(String name) {
            this.name = name;
            return this;
        }

        public Builder description(String description) {
            this.description = description;
            return this;
        }

        public Builder SystemPrompt(String SystemPrompt) {
            this.SystemPrompt = SystemPrompt;
            return this;
        }

        public Builder nextStepPrompt(String nextStepPrompt) {
            this.nextStepPrompt = nextStepPrompt;
            return this;
        }

        public Builder stuckPrompt(String stuckPrompt) {
            this.stuckPrompt = stuckPrompt;
            return this;
        }

        public Builder chatClient(ChatClient chatClient) {
            this.chatClient = chatClient;
            return this;
        }

        public Builder availableTools(ToolCallback[] allTools) {
            this.availableTools = allTools;
            return this;
        }

        public Builder maxStep(Integer maxStep) {
            this.maxStep = maxStep;
            return this;
        }

        public SmhAgent build() {
            SmhAgent smhAgent = new SmhAgent(availableTools);
            smhAgent.setName(name);
            smhAgent.setDescription(description);
            smhAgent.setSystemPrompt(SystemPrompt);
            smhAgent.setNextStepPrompt(nextStepPrompt);
            smhAgent.setNextStepPromptReplica(nextStepPrompt);
            smhAgent.setStuckPrompt(stuckPrompt);
            smhAgent.setChatClient(chatClient);
            smhAgent.setMaxStep(maxStep);
            return smhAgent;
        }

    }
}
